package com.example.demogooglemapsv2;

/*import com.example.androidhive.Base_;
import com.example.androidhive.Logowanie;
import com.example.androidhive.R;*/
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity implements
//OnMapClickListener, OnMapLongClickListener {
OnMarkerClickListener {
	private final LatLng LOCATION_BURNABY = new LatLng(50.97235, 18.21807);
	private GoogleMap map;/*Location location*/;
	 private LocationManager locationManager;String bestProvider;
	 private static final long MIN_TIME = 400;	JSONParser jsonParser = new JSONParser();
		int wynik;
	 private static final float MIN_DISTANCE = 1000;
	 double lat,lon,latitude = 0;
		double longitude = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// com.google.android.maps.MapView.LayoutParams params=new
		// com.google.android.maps.MapView.LayoutParams(500,200, null, 0);
		// map.setLayoutParams(params);
		// map.invalidate();
		/*Button c = (Button) findViewById(R.id.button1);
		c.setWidth(90);
		c.setHeight(30);
		Button c1 = (Button) findViewById(R.id.button2);
		c1.setWidth(90);
		c1.setHeight(30);*/
		 map = ((MapFragment) getFragmentManager()
					.findFragmentById(R.id.map)).getMap();
		//map.setOnMapLongClickListener(this);
		 map.setPadding(0, 50, 0, 100);
		 map.setOnMarkerClickListener(this);
		// map.
		// map.addMarker(new
		// MarkerOptions().position(LOCATION_BURNABY).title("TUT"));

		// Get a handle to the Map Fragment
		// GoogleMap map = ((MapFragment) getFragmentManager()
		// .findFragmentById(R.id.map)).getMap();

		// LatLng sydney = new LatLng(-33.867, 151.206);

		// map.setMyLocationEnabled(true);
		// map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

		// map.addMarker(new MarkerOptions()
		// .title("Sydney")
		// .snippet("The most populous city in Australia.")
		// .position(sydney));
		
		//addd(50.98138, 18.21434, "mojdom");
	//addd(51.00274, 18.15491, "smardy");
		// create marker
  /*  if (map != null) {
        map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }*/
/*
map.setMyLocationEnabled(true);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
 bestProvider = locationManager.getBestProvider(criteria, false);
 location = locationManager.getLastKnownLocation(bestProvider);
LocationListener loc_listener = new LocationListener() {

    public void onLocationChanged(Location l) {}

    public void onProviderEnabled(String p) {}

    public void onProviderDisabled(String p) {}

    public void onStatusChanged(String p, int status, Bundle extras) {}
};*/
/*//locationManager.requestLocationUpdates(bestProvider, 0, 0, loc_listener);
location = locationManager.getLastKnownLocation(bestProvider);
try {
    lat = location.getLatitude();
    lon = location.getLongitude();
} catch (NullPointerException e) {
    lat = -1.0;
    lon = -1.0;
}
Toast.makeText(this, "Myd"+lat+"-"+lon, Toast.LENGTH_LONG).show();*/
		/*
		 * 
		 * PolygonOptions rectOptions = new PolygonOptions() .add(new
		 * LatLng(37.35, -122.0), new LatLng(37.45, -122.0), new LatLng(37.45,
		 * -122.2), new LatLng(37.35, -122.2), new LatLng(37.35, -122.0));
		 */

		// Get back the mutable Polygon
		// Polygon polygon = map.addPolygon(rectOptions);
Button btnCreateProduct1 = (Button) findViewById(R.id.button2);
btnCreateProduct1.setOnClickListener(new View.OnClickListener() {
	@Override
	public void onClick(View view) {
	//	Editable aa1 = ((EditText) findViewById(R.id.mojekontohaslo)).getText();
	//	final String b=		aa1.toString();
	//	String[] c={"logowanie","nazwa-"+b+" mm-o"};
	//	Base_ aa=new Base_(Logowanie.this,c,c_);
	//	aa.execute();

map.setMyLocationEnabled(true);

/*locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
 bestProvider = locationManager.getBestProvider(criteria, false);
LocationListener loc_listener = new LocationListener() {

    public void onLocationChanged(Location l) {}

    public void onProviderEnabled(String p) {}

    public void onProviderDisabled(String p) {}

    public void onStatusChanged(String p, int status, Bundle extras) {}
};
Location location = locationManager.getLastKnownLocation(bestProvider);
		try {
		    lat = location.getLatitude();
		    lon = location.getLongitude();
		} catch (NullPointerException e) {
		    lat = -1.0;
		    lon = -1.0;
		}
		b(location);*/
LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria cr = new Criteria();
String provider = locationmanager.getBestProvider(cr, false);
Location location = locationmanager.getLastKnownLocation(provider);
		
if (location != null) {
    double lat=location.getLatitude();
double lng=location.getLongitude();
	 a(lat,lng);
}
	}


});
	}
	void b(Location location){
		EditText tvLocation = (EditText) findViewById(R.id.editText1);

	        // Getting latitude of the current location
	        double latitude = location.getLatitude();

	        // Getting longitude of the current location
	        double longitude = location.getLongitude();

	        // Creating a LatLng object for the current location
	        LatLng latLng = new LatLng(latitude, longitude);

	        // Showing the current location in Google Map
	        map.moveCamera(CameraUpdateFactory.newLatLng(latLng));

	        // Zoom in the Google Map
	        map.animateCamera(CameraUpdateFactory.zoomTo(15));

	        // Setting latitude and longitude in the TextView tv_location
	        tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );

	}
	void a(Double a,Double b){
		Toast.makeText(this, "Myd"+a+"-"+b, Toast.LENGTH_LONG).show();
		String[] c={"wspolrzedne","a-"+a+" b-"+b};
		Base_ aa=new Base_(MainActivity.this,c,getBaseContext());
		aa.execute();
	}
	class Base_ extends AsyncTask<String,String,String>{
		private ProgressDialog pDialog;
		Activity aa;
		public void setAa(Activity aa) {
			this.aa = aa;
		}
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		JSONParser jsonParser = new JSONParser();
		int wynik;
		
		public int getWynik() {
			return wynik;
		}
		/*String url(String a){
			if(a!="" && a!=null)
				return new BASE().metoda(a);
			else
				return "";
		}*/
		Context ct;
		public Base_(Activity aa,String[] aaa,Context ctx) {
			this.aa = aa;
			this.aaa = aaa;
			this.ct=ctx;
		}
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			/*pDialog = new ProgressDialog(aa);
			pDialog.setMessage((aaa[0].substring(0,1)).toUpperCase()+aaa[0].substring(1)+"..");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();*/
		}
		String[] aaa;
		public void setAaa(String[] aaa) {
			this.aaa = aaa;
		}
		@Override
		protected String doInBackground(String... arg0) {
			String[] cc=aaa[1].split(" ");
			for (String c:cc){
				String[] aa=c.split("-");
				params.add(new BasicNameValuePair(aa[0],aa[1]));
			}
			JSONObject json = jsonParser.makeHttpRequest("http://opiekuj1.javaprovider.net/php/dane.php",
					"POST", params);
			try {
				int success = json.getInt("success");
				wynik=success;	Intent i;
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}
		void a(){
			
		}
		protected void onPostExecute(String file_url) {
			//pDialog.dismiss();
		}

	}
	void addd(double a, double b, String c) {
		MarkerOptions marker = new MarkerOptions().position(new LatLng(a, b))
				.title(c);
		map.addMarker(marker);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void onClick_Przycisk_MAP_TYPE_SATELLITE(View v) {
		map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
		CameraUpdate up = CameraUpdateFactory.newLatLngZoom(LOCATION_BURNABY,
				16);
		map.animateCamera(up);
	}

	public void onClick_Przycisk_MAP_TYPE_TERRAIN(View v) {
		// map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
		// CameraUpdate
		// up=CameraUpdateFactory.newLatLngZoom(LOCATION_BURNABY,14);
		// map.animateCamera(up);
		SmsManager smsManager = SmsManager.getDefault();
		smsManager.sendTextMessage("500721601", null,
				"NIE ODPISUJ TYLKO DAJ ZNAC ZE DOSZEDL.MATEUSZ", null, null);
	}

	public boolean onMarkerClick(Marker arg0) {
		//if (arg0.getTitle().equals("mojdom")) {
		//	Button c = (Button) findViewById(R.id.button1);
		//	c.setText(arg0.getTitle());
		//}

		// if(arg0.getTitle().equals("Hello Maps ")) // if marker source is
		// clicked
		// Toast.makeText(MainActivity.this, arg0.getTitle(),2000).show();//
		// display toast
		return true;
		// return false;
	}

	


}
